import time

import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from tests.litemall_sys.pages.add_goods import AddGoods
from tests.litemall_sys.pages.basepage import BasePage


class GoodsList(BasePage):
    __ele = (By.XPATH, '//*[text()="商品列表"]')
    __goodname = (By.CSS_SELECTOR, 'tbody>tr>td:nth-child(3)>div')
    delect_good = (By.CSS_SELECTOR, 'tbody>tr:nth-child(1)>td:nth-child(12)>div:nth-child(1)>button:nth-child(2)')
    del_pass = (By.XPATH, '//*[text()="删除成功"]')
    ele = (By.XPATH, '//*[text()="当前价格"]')
    tow_page = (By.CSS_SELECTOR, '.el-pager>li:nth-child(2)')
    find_name = (By.CSS_SELECTOR, '[placeholder="请输入商品名称"]')
    find_method = (By.CSS_SELECTOR, '.filter-container>button:nth-child(4)')

    def goto_add_goods(self):
        with allure.step("点击商品列表，进入商品列表页面"):
            self.find_click(*self.__ele)
        return AddGoods(self.driver)

    def get_goods_list(self):
        with allure.step("获取页面的商品名称"):
            el_list = self.finds(self.__goodname)
            name_list = [el.text for el in el_list]
        return name_list

    def del_goods(self):
        with allure.step("点击商品列表，进入商品列表页面"):
            self.find_click(*self.__ele)
        with allure.step("点击删除"):
            self.find_click(*self.delect_good)
            self.wait(expected_conditions.visibility_of_element_located(self.del_pass))
            res = self.find(*self.del_pass).text
        return res

    def find_goods_name(self, name):
        with allure.step("点击商品列表，进入商品列表页面"):
            self.find_click(*self.__ele)
        with allure.step("输入商品名称"):
            self.find_and_send(*self.find_name, text=name)
        with allure.step("点击查找"):
            self.find_click(*self.find_method)
        with allure.step("获取页面的商品名称"):
            time.sleep(5)
            el_list = self.finds(*self.__goodname)
            name_list = [el.text for el in el_list]
        return name_list



